"""
selenium基类
本文件存放了selenium基类的封装方法
"""
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
from common.readelement import Element

from config.conf import cm
from utils.times import sleep
from utils.logger import log
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium import webdriver

class WebPage(object):
    """selenium基类"""

    def __init__(self, driver):
        # self.driver = webdriver.Chrome()
        self.driver = driver
        self.timeout = 20
        self.wait = WebDriverWait(self.driver, self.timeout)

    def get_url(self, url):
        """打开网址并验证"""
        self.driver.maximize_window()
        self.driver.set_page_load_timeout(60)
        try:
            self.driver.get(url)
            self.driver.implicitly_wait(10)
            log.info("打开网页：%s" % url)
        except TimeoutException:
            raise TimeoutException("打开%s超时请检查网络或网址服务器" % url)

    def getLocator(self,name,value):
        """元素定位器（全部）"""
        if name == "id":
            return self.driver.find_element_by_id(value)
        elif name == "classname":
            return self.driver.find_element_by_class_name(value)
        elif name == "name":
            return self.driver.find_element_by_name(value)
        elif name == "xpath":
            return self.driver.find_element_by_xpath(value)
        elif name == "linkText":
            return self.driver.find_element_by_link_text(value)
        elif name == "tag":
            return self.driver.find_element_by_tag_name(value)
        elif name == "css":
            return self.driver.find_element_by_css_selector(value)
        else:
            log(Exception)


    def input_text(self,name,value,txt):
        """输入(输入前先清空)"""
        search = Element('search')
        sleep(0.5)
        ele = WebPage.getLocator(self,name,value)
        ele.clear()
        ele.send_keys(txt)
        log.info("输入文本：{}".format(txt))


    def is_click(self, name,value):
        """点击"""
        search = Element('search')
        sleep(0.5)
        ele = WebPage.getLocator(self, name, value)
        ele.click()
        sleep()
        log.info("点击元素：{}".format(name,value))

    def element_text(self, name,value):
        """获取当前的text"""
        search = Element('search')
        _text = WebPage.getLocator(self, name, value).text
        log.info("获取文本：{}".format(_text))
        return _text

    def click_enter(self, name,value):
        """点击Enter键"""
        search = Element('search')
        sleep(0.5)
        sc = WebPage.getLocator(self, name, value)
        sc.send_keys(Keys.ENTER)
        sleep()

    def send_BackSpace(self, name,value):
        """模拟删除键BackSpace"""
        search = Element('search')
        sleep(0.5)
        sc = WebPage.getLocator(self, name, value)
        sc.send_keys(Keys.BACK_SPACE)
        sleep()

    def sc_fg(self, name,value,txt):
        """上传"""

        search = Element('search')
        sleep(0.5)
        sc = WebPage.getLocator(self, name, value)
        #js = "document.getElementsByTagName('input')[0].style.display='block';"
        #self.driver.execute_script(js)
        #sc.send_keys("E:\\图片集市项目\\测试图片\\tp\\test.jpeg")
        sc.send_keys(txt)
        sleep()


    @property
    def get_source(self):
        """获取页面源代码"""
        return self.driver.page_source

    def refresh(self):
        """刷新页面F5"""
        self.driver.refresh()
        self.driver.implicitly_wait(30)

